package be.felnederlands.client.navigation;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

public class NavigationPanel implements ClickHandler, HistoryListener{

	public enum NavigationElement{
		HOME, NEWWORD;
	}
	
	private List<NavigationListener> navigationListeners = new ArrayList<NavigationListener>();
	private Anchor home;
	private Anchor newWordLink;
	
	public NavigationPanel(){
		Panel homeLink = RootPanel.get("home");
		Panel newWordLinkPanel =  RootPanel.get("registerNewWord");
		
		home = new Anchor("Home");
		newWordLink = new Anchor("Registreer niew woord");
		
		homeLink.add(home);
		newWordLinkPanel.add(newWordLink);
		
		home.addClickHandler(this);
		newWordLink.addClickHandler(this);
	}

	@Override
	public void onClick(ClickEvent event) {
		if(event.getSource().equals(home))
			notifyNavigationListeners(NavigationElement.HOME);
		if(event.getSource().equals(newWordLink))
			notifyNavigationListeners(NavigationElement.NEWWORD);
	}
	
	private void notifyNavigationListeners(NavigationElement navigationElement) {
		for (NavigationListener navigationListener : navigationListeners) {
			navigationListener.navigateTo(navigationElement);
		}
		
		History.newItem(navigationElement.name());
	}
	
	public void registerNavigationListener(NavigationListener navigationListener){
		this.navigationListeners.add(navigationListener);
	}

	@Override
	public void onHistoryChanged(String historyToken) {
		notifyNavigationListeners(NavigationElement.valueOf(historyToken));
	}
	
}
